#include "StdAfx.h"
#include "EnvironmentCheck.h"


#include "Command.h"
#include "LogAction.h"
#include "Suit.h"
#include "Step.h"
#include "Case.h"
#include "Manage.h"
#include "../macro.h"

#include <filesystem>

EnvironmentCheck::EnvironmentCheck(void)
{
}

EnvironmentCheck::~EnvironmentCheck(void)
{
}

bool EnvironmentCheck::run(void* param)
{

	DBG_PRINTF("%s\r\n",__FUNCTION__);
	std::filesystem::path config_path("./autosuit/", std::filesystem::path::native_format);
	if (std::filesystem::exists(config_path))
	{
		std::filesystem::directory_iterator item_iter(config_path);
		std::filesystem::directory_iterator item_end;
		for (; item_iter != item_end; ++item_iter)
		{
			if (!std::filesystem::is_directory(*item_iter))
			{
				// ATLTRACE(L"%s\n", item_iter->path().c_str() );
				std::string config_file_name = item_iter->path().string();
				parseConfig(config_file_name.c_str());
			}
		}
	}

	return true;
}

bool EnvironmentCheck::parseConfig(const char* path)
{
	TiXmlDocument doc;
	if (!doc.LoadFile(path))
	{
		return false;
	}

	TiXmlElement* pRoot = doc.RootElement();
	if (!pRoot)
	{
		return false;
	}

	const char* p = pRoot->Attribute("name");
	Suit* pSuit = new Suit(p);
	// Get test case
	for (TiXmlNode* pTestCase = pRoot->FirstChild("test_case"); pTestCase; pTestCase = pTestCase->NextSibling("test_case"))
	{
		const char* pCaseName = pTestCase->ToElement()->Attribute("name");
		if (pCaseName == NULL)
		{
			pCaseName = "";
		}
		Case* pCase = new Case(pCaseName, pSuit);
		parseLoopable(pTestCase, pCase);

		pSuit->addCase(pCase);
		for (TiXmlNode* pXmlStep = pTestCase->FirstChild("step"); pXmlStep; pXmlStep = pXmlStep->NextSibling("step"))
		{
			const char* pStepName = pXmlStep->ToElement()->Attribute("message");
			if (pStepName == NULL)
			{
				pStepName = "";
			}
			Step* pStep = new Step(pCase);
			parseLoopable(pXmlStep, pStep);

			pCase->addStep(pStep);
			for (TiXmlNode* pXmlAction = pXmlStep->FirstChild(); pXmlAction; pXmlAction = pXmlAction->NextSibling())
			{
				if (pXmlAction)
				{
					const char * pName = pXmlAction->Value();
					if (_stricmp(pName, "command") == 0)
					{
						const char*pCommandName = pXmlAction->ToElement()->GetText();
						int nDelay_after = 0;
						int nDelay_before = 0;
						pXmlAction->ToElement()->Attribute("delay_after", &nDelay_after);
						pXmlAction->ToElement()->Attribute("delay_before", &nDelay_before);

						Action* pAction = new Command(pCommandName, nDelay_after, nDelay_before);

						pStep->addAction(pAction);
					}
					else if (_stricmp(pName, "log") == 0)
					{
						const char* pKeyWord = pXmlAction->ToElement()->Attribute("keyword");
						if (pKeyWord == NULL)
						{
							pKeyWord = "";
						}

						int nTimeOut = 0;
						pXmlAction->ToElement()->Attribute("timeout", &nTimeOut);
						Action* pAction = new LogAction(pKeyWord, nTimeOut);

						pStep->addAction(pAction);
					}
				}
			}
		}
	}
	Manage& manager = Manage::getInstance();
	manager.addSuit(pSuit);

	return true;
}

bool EnvironmentCheck::parseLoopable(TiXmlNode* pXmlLooper, loopable* looper)
{
	const char* pStart = pXmlLooper->ToElement()->Attribute("start");
	const char* pStep = pXmlLooper->ToElement()->Attribute("step");
	const char* pEnd = pXmlLooper->ToElement()->Attribute("end");
	if (pStart == NULL || pStep == NULL || pEnd == NULL)
	{
		return false;
	}

	int start = atoi(pStart);
	int step = atoi(pStep);
	int end = atoi(pEnd);
	looper->set(start, step, end);

	const char* pIndexName = pXmlLooper->ToElement()->Attribute("index_name");
	if (pIndexName == NULL)
	{
		return false;
	}

	looper->setIndexName(StringToWstring(std::string(pIndexName)));
	return true;
}